package edu.gduf.controller;

import com.github.pagehelper.PageInfo;
import edu.gduf.domain.Message;
import edu.gduf.domain.ResultInfo;
import edu.gduf.service.MessageService;
import edu.gduf.service.UserService;
import edu.gduf.utils.HtmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author kgy
 * @description: 消息的控制器
 */
@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;
    /**
     * 返回发送消息的页面
     * @param friendName
     * @param model
     * @return
     */
    @RequestMapping(value = "send",method = RequestMethod.GET)
    public String sendMessage(@RequestParam(value = "friendname",required = false) String friendName, Model model){
        if(friendName != "" && friendName!=null)
            model.addAttribute("friendname",friendName);
        return "message/send";
    }

    /**
     * 处理发送消息的表单
     * @param message
     * @param request
     * @return
     */
    @RequestMapping(value = "send",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo sendMessage(@RequestBody Message message,
                              HttpServletRequest request){
        System.out.println(message);
        // 1,获取当前登陆的用户名
        String username =(String) request.getSession().getAttribute("username");
        if(message.getFriendName() == ""){
            return ResultInfo.failInfo("收信人不能为空");
        }else if(message.getContent() == ""){
            return ResultInfo.failInfo("不能发送空消息");
        }else if(message.getFriendName().equals(username))
            return ResultInfo.failInfo("收信人不能为本人");
        else if(userService.findUserByUserName(message.getFriendName()) == null)
            return ResultInfo.failInfo("收信人不存在");
        else{
            //2.创建Message对象封装数据
            message.setFromName(username);
            message.setToName(message.getFriendName());
            message.setOwner(username);
            message.setContent(HtmlUtil.delHTMLTag(message.getContent()));//进行内容处理，保留img表情
            message.setStatus(2); // 设置成已读状态
            message.setType(1); // 设置为私信类型
            message.setCreateTime(System.currentTimeMillis()/1000L); // 设置当前时间
            return messageService.sendMessage(message);
        }
    }

    /**
     * 返回用户的私信列表
     * @param request
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String listmessage(HttpServletRequest request,@RequestParam(value = "pageIndex",required = false)Integer pageIndex,Model model){
        // 1.获取当前登陆用户的用户名
        String username =(String) request.getSession().getAttribute("username");
        // 2.获取聊天列表
        if(pageIndex == null || pageIndex < 1)
            pageIndex = 1;
        PageInfo info = messageService.getMessageList(pageIndex, username);
        // 3.将聊天列表数据加入到request域中
        model.addAttribute("msglist",info);
        return "/message/list";
    }

    /**
     * 查看用户与用户间的聊天详情
     * @param friendname
     * @param request
     * @return
     */
    @RequestMapping(value = "viewdetail",method = RequestMethod.GET)
    public String viewdetail(@RequestParam("friendname")String friendname,@RequestParam(value = "pageIndex",required = false)Integer pageIndex, HttpServletRequest request){
        if(pageIndex == null)
            pageIndex = 1;
        // 1. 从session域中获取用户名
        String username = (String) request.getSession().getAttribute("username");
        // 2.
        PageInfo info = messageService.viewMessageDetail(pageIndex, username, friendname);
        request.setAttribute("friendname",friendname);
        request.setAttribute("msglist",info);
        return "message/detail";
    }

    @RequestMapping(value = "getunreadnum",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo getUnReadNum(HttpServletRequest request){
        String username =(String) request.getSession().getAttribute("username");
        if(username == null)
            return null;
        else
            return ResultInfo.successInfo(messageService.getUnreadNum(username).toString());
    }


    /**
     * 删除某个消息
     * @param mid
     * @param request
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public String deletemessage(@RequestParam("mid")Integer mid,HttpServletRequest request){
        return "";
    }

    /**
     * 删除所有消息
     * @param friendname
     * @param request
     * @return
     */
    @RequestMapping(value = "deleteAll",method = RequestMethod.POST)
    public String deleteAllMessage(@RequestParam("friendname")String friendname,HttpServletRequest request){
        String username =(String) request.getSession().getAttribute("username");
        ResultInfo resultInfo = messageService.deleteAllMessageByOF(username, friendname);
        return "";
    }
}
